package com.example.testrxjava;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.testrxjava.util.ConTract;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;


public class MainActivity extends AppCompatActivity {
    int drable=R.mipmap.ic_launcher;
    ImageView imageView;
    TextView tv;
    TagFlowLayout lv;
    List<GZCode> data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        data=new ArrayList<GZCode>();
//        
        lv=(TagFlowLayout) findViewById(R.id.layout);
//        getImage();
//        changeType();
        doRequestByRxRetrofit();


    }

    private void initLlyaout() {
        List<GZCode.DataBean> list=new ArrayList<GZCode.DataBean>();
        for (int i = 0; i < data.size(); i++) {
            list=data.get(i).getData();
        }

        lv.setAdapter(new TagAdapter<GZCode.DataBean>(list) {
            @Override
            public View getView(FlowLayout parent, int position, GZCode.DataBean dataBean) {
                TextView tv = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.flow_layout_tv,
                        parent, false);
                String name = null;
//                    for (int j = 0; j < dataBean.getData().size(); j++) {
                Log.e("===",position+"");
                Log.e("d--",dataBean.getName());
                name = dataBean.getName();
                tv.setText(name);
                tv.setBackgroundColor(randomTagColor());
                Log.e("name===",name);




//                }

                return tv;
            }




        });

    }
    /**
     * 搜索标签颜色
     */
    public static final int[] TAB_COLORS = new int[]{
            Color.parseColor("#90C5F0"),
            Color.parseColor("#91CED5"),
            Color.parseColor("#F88F55"),
            Color.parseColor("#C0AFD0"),
            Color.parseColor("#E78F8F"),
            Color.parseColor("#67CCB7"),
            Color.parseColor("#F6BC7E")
    };
    public static int randomTagColor() {
        int randomNum = new Random().nextInt();
        int position = randomNum %TAB_COLORS.length;
        if (position < 0) {
            position = -position;
        }
        return TAB_COLORS[position];
    }

    private void doRequestByRxRetrofit() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())//设置Json转换器
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//Rxjava适配器
                .build();
        RetrofitServer retrofitServer=retrofit.create(RetrofitServer.class);
        retrofitServer.getMessage()
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Observer<GZCode>() {
                    @Override
                    public void onCompleted() {
                        initLlyaout();

                    }
        
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(GZCode gzCode) {
                        GZCode.DataBean bean = null;
                        for (int i = 0; i < gzCode.getData().size(); i++) {
                            Log.e("公众号---",gzCode.getData().get(i).getName());
        //                  bean =gzCode.getData().get(i);
                        }
        //                data.add(gzCode);
                        data.add(gzCode);
                        for (int i = 0; i < gzCode.getData().size(); i++) {
                        }



                    }
                });






    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            return null;
        }
    }
    private void changeType() {
        Observable.just("22","33")
                .map(new Func1<String,Integer>() {
                    @Override
                    public Integer call(String s) {
                        return Integer.parseInt(s);


                    }
                }).subscribe(new Observer<Integer>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Integer s) {
                Log.e("类型转换===",s+"");
            }
        });


    }


    private void getImage() {
      Observable.create(new Observable.OnSubscribe<Drawable>() {
          @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
          @Override
          public void call(Subscriber<? super Drawable> subscriber) {
            Drawable drawable=getTheme().getDrawable(drable);
            subscriber.onNext(drawable);
            subscriber.onCompleted();
          }
      }).subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new Observer<Drawable>() {
          @Override
          public void onCompleted() {

          }

          @Override
          public void onError(Throwable e) {

          }

          @Override
          public void onNext(Drawable drawable) {
//            imageView.setImageDrawable(drawable);
          }
      });

    }

    private void init() {
        //Observable  被观察者
        Observable observable= Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("hello");
                subscriber.onNext("soga");
                subscriber.onNext("yoxi");
                subscriber.onCompleted();
            }

        });
        //Observer  观察者
        Observer<String> observer=new Observer<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                Log.e("被观察者发送的消息==",s);
            }

        };
        observable.subscribe(observer);

    }
}
